//
//  GeDiaoViewController.m
//  Style
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#define ScreenWidth          [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight         [[UIScreen mainScreen] bounds].size.height
#define ViewWidth(v)                        v.frame.size.width
#define ViewHeight(v)                       v.frame.size.height

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

typedef enum {
    RotateStateStop,
    RotateStateRunning,
}RotateState;

#import "GeDiaoViewController.h"
#import "AFNetWorkingW.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <STKAudioPlayer.h>
#import "YYPlay.h"
#import "YYPictureViewController.h"

@interface GeDiaoViewController ()

@property (strong, nonatomic) MPMoviePlayerController *streamPlayer;

@property (nonatomic, assign) NSInteger isPlayBack;

@property (nonatomic, retain) NSTimer *timer;

@property (nonatomic, retain) UIImageView *imageview;

@property (nonatomic, retain) UIButton *button;

@property (nonatomic, assign) RotateState *rotateState;

@property (nonatomic, assign) CGFloat imageviewAngle;

@property (nonatomic, retain) STKAudioPlayer *stk;


@end

@implementation GeDiaoViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"格调";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    self.imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Music"]];
    self.imageview.autoresizingMask = UIViewAutoresizingNone;
    self.imageview.contentMode = UIViewContentModeScaleToFill;
    self.imageview.bounds = CGRectMake(ScreenWidth - 40, 0, 30, 30);
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, 40, 40);
    [self.button addSubview:self.imageview];
    [self.button addTarget:self action:@selector(animate:) forControlEvents:UIControlEventTouchUpInside];
    self.button.tag = 100;
    self.imageview.center = self.button.center;
    
    /* 自定义 BarButtonItem */
    self.right = [[UIBarButtonItem alloc] initWithCustomView:self.button];
    self.navigationItem.rightBarButtonItem = self.right;
    self.right.tintColor = [UIColor blackColor];
    [self mainScrollViewSubviews];
 
    /* 获取系统当前日期 */
    NSDate *senddate = [NSDate date];
    NSDateFormatter  *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"MMdd"];
    self.locationString = [dateformatter stringFromDate:senddate];
    
    
    NSNotificationCenter *ViewCenter = [NSNotificationCenter defaultCenter];
    [ViewCenter addObserver:self selector:@selector(passView:) name:@"view" object:nil];
  
}


- (void)animate:(UIButton *)button
{

    button.selected = !button.selected;
    
//    改变ImageView旋转状态
    if ( button.selected == 1 ) {
        if (self.Player1.progress != 0) {
            [self.Player1 resume];
            self.rotateState = RotateStateRunning;
            [self rotateAnimate];
        }
        else{
        self.rotateState = RotateStateRunning;
        [self rotateAnimate];
        
        NSString *urlStr = [NSString stringWithFormat:@"http://nextday-file.b0.upaiyun.com/2015%@/%@_conv.mp3", self.locationString, self.locationString];
        
        self.Player1 = [[STKAudioPlayer alloc]init];
        [self.Player1 playURL:[NSURL URLWithString:urlStr]];
      
        }

    }else{
        
        self.rotateState = RotateStateStop;
        [self.Player1  pause];
       
    }

}

-(void)rotateAnimate{
    self.imageviewAngle += 50;
    //0.5秒旋转50度
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.imageview.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.imageviewAngle));
    } completion:^(BOOL finished) {
        if (self.rotateState == RotateStateRunning) {
            [self rotateAnimate];
        }
    }];
}

- (void)mainScrollViewSubviews
{
    self.GEDIAOScrollView = [[GeDiaoScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.GEDIAOScrollView.contentSize = CGSizeMake(ScreenWidth * 10, 0);
    [self.view addSubview:self.GEDIAOScrollView];
    
    [self.GEDIAOScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:@"new"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    CGPoint p = [change[@"new"] CGPointValue];
    NSInteger i = p.x / ScreenWidth;
    
    self.sWebLk = [self.mArr objectAtIndex: i];
}


- (void)passView:(NSNotification *)view
{
    YYPictureViewController *pic = [[YYPictureViewController alloc] init];
    pic.strOriginalImgUrlView = view.object;
    [self.navigationController pushViewController:pic animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
